package file_handler;

import java.io.*;
import java.net.URL;

/*
 * This Class provides functionality to handle file read/write operations
 * @author Hammad Afzal
 */

public class File_Manager
{

    /*
     * It reads the file present in given (local to disk) path.
     */
    public StringBuffer fileReader(String input_path)
    {
        File readFile = new File(input_path);
        StringBuffer contents = new StringBuffer();
        BufferedReader input = null;
        try
        {
            input = new BufferedReader(new FileReader(readFile));
            String line = null;
            while ((line = input.readLine()) != null)
            {
                contents.append(line);
                contents.append(System.getProperty("line.separator"));
            }
        }
        catch (FileNotFoundException ex)
        {
            ex.printStackTrace();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                if (input != null)
                {
                    input.close();
                }
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
        return contents;
    }

    /*
     * It downloads the file in given URL string, and stores it in path (local on disk)
     */
    public void fileDownloader(String url_string, String output_path)
    {
        try
        {
            URL url = new URL(url_string);
            InputStream urlStream = url.openStream();
            InputStreamReader iStr = new InputStreamReader(urlStream);
            BufferedReader reader = new BufferedReader(iStr);

            File file1 = new File(output_path);
            OutputStream fout = new FileOutputStream(file1);
            OutputStream bout = new BufferedOutputStream(fout);
            OutputStreamWriter writer = new OutputStreamWriter(bout,
                    "ISO-8859-1");
            String line = reader.readLine();

            while (line != null)
            {
                writer.write(line + "\n");
                line = reader.readLine();
            }
            urlStream.close();
            reader.close();
            writer.close();
        }
        catch (IOException e)
        {
        }
    }
    
    /*
     * It writes the contents of StringBuffer in given path
     */
    
    public void fileWriter(StringBuffer stringBuffer,String output_path)
    {
        try
        {
            File writeFile = new File(output_path);
            OutputStream fout = new FileOutputStream(writeFile);
            OutputStream bout = new BufferedOutputStream(fout);
            OutputStreamWriter writer = new OutputStreamWriter(bout);
            writer.write(stringBuffer.toString());
            writer.close();
            bout.close();
            fout.close();
        }
        catch(Exception ex)
        {
            System.out.println("Value of Exception is :" + ex.getMessage());
        }
    }

    /*
     * It writes the contents of string in given path
     */

    public void fileWriter(String contents,String output_path)
    {
        try
        {
            File writeFile = new File(output_path);
            OutputStream fout = new FileOutputStream(writeFile);
            OutputStreamWriter writer = new OutputStreamWriter(fout,"ISO-8859-1");
            writer.write(contents.toString());
            writer.close();
            fout.close();
        }
        catch(Exception ex)
        {
            System.out.println("Value of Exception is :" + ex.getMessage());
        }
    }
}
